<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Controller;
use think\Request;

class Bank extends  Api
{
    protected $model;
    //    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();

        $this->model = model('bank');
    }

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $list = $this->model->where('user_id', $this->auth->id)->select();
        if ($list) {
            foreach ($list as $k => $v) {
                $list[$k]['bank_code'] = substr($list[$k]['bank_code'], 0, 4) . ' ' . str_repeat('*', strlen($list[$k]['bank_code']) - 8) . ' ' . substr($list[$k]['bank_code'], -4);
            }
        }
        $this->success('success', $list);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $user = $this->auth->getUser();
        $request = $this->request->request();

        if (!$request['real_name']) $this->error('请输入姓名');
        if (!$request['id_card']) $this->error('请输入身份证号');
        if (!$request['bank_name']) $this->error('请输入开户行');
        if (!$request['bank_code']) $this->error('银行卡号');
        if (!$request['branch_name']) $this->error('请输入开户支行');
        if (!$request['mobile']) $this->error('请输入手机号');



        $user->real_name = $request['real_name'];
        $user->id_card = $request['id_card'];
        $user->save();

        $insert['bank_name'] = $request['bank_name'];
        $insert['bank_code'] = $request['bank_code'];
        $insert['branch_name'] = $request['branch_name'];
        $insert['mobile'] = $request['mobile'];
        $insert['createtime'] = time();
        $insert['user_id'] = $this->auth->id;
        $cardNumber = $insert['bank_code'];

        $len = strlen($cardNumber);
        if ($len < 12) {
            $this->error('银行卡号不正确');
        }
        $this->model->save($insert);
        $this->success('添加成功');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id = '')
    {
        if ($id) {
            $row = $this->model->where('id', $id)
                ->where('user_id', $this->auth->id)
                ->find();
        } else {
            $row = $this->model
                ->where('user_id', $this->auth->id)
                ->order('id asc')
                ->find();
        }


        $row['bank_code'] = substr($row['bank_code'], 0, 4) . ' ' . str_repeat('*', strlen($row['bank_code']) - 8) . ' ' . substr($row['bank_code'], -4);
        $this->success('success', $row);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $bool = $this->model->where('id', $id)
            ->where('user_id', $this->auth->id)
            ->delete();
        $this->success('删除成功');
    }

    public function withdrawal()
    {
        $user = $this->auth->getUser();
        $bank_id = $this->request->request('bank_id', 0);
        $money = $this->request->request('money', 0);

        $bank = $this->model->where('id', $bank_id)
            ->find();
        if (! $bank) {
            $this->error('银行卡不存在');
        }

        if ($money > $user['money']) {
            $this->error('余额不足');
        }

        if ($money <= 0) {
            $this->error('输入金额不正确');
        }

        $insert = [
            'user_id' => $user['id'],
            'money' => $money,
            'bank_name' => $bank['bank_name'],
            'bank_code' => $bank['bank_code'],
            'branch_name' => $bank['branch_name'],
            'mobile' => $bank['mobile'],
            'createtime' => $bank['createtime'],
        ];

        $bool = model('withdrawal')->insert($insert);
        if ($bool) {
            model('user')::money('', -$money, $user['id'], '申请提现', 2);
            $this->success('申请成功');
        } else {
            $this->success('提现异常');
        }
    }
}
